package com.diyunapp.xilinapp.me;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.CodeCountDown;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhoneCerActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.phone_cer_edt)
    EditText phoneCerEdt;
    @Bind(R.id.phone_code_et)
    EditText phoneCodeEt;
    @Bind(R.id.getCode_tv)
    TextView getCodeTv;
    @Bind(R.id.ceremail_right_away)
    TextView ceremailRightAway;
    private String codeRandom;
    private String username;
    private String member_id;
    @BindColor(R.color.colorDarkTopBar)
    int pressColor;
    @BindColor(R.color.colorText0)
    int unpressColor;
    private String type;
    private String num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_cer);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.phcer_menubar_inc);
        super.MenuWidget(headView);
        SharedPreferences userinfo=getSharedPreferences("user_info",MODE_PRIVATE);
        username=userinfo.getString("username","");
        member_id=userinfo.getString("member_id","");
    }
    @OnClick({R.id.go_back, R.id.getCode_tv, R.id.ceremail_right_away})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
            case R.id.getCode_tv:
                getCodeTv.setClickable(false);
                getCodeTv.setTextColor(unpressColor);
                CodeCountDown.with(PhoneCerActivity.this).into(getCodeTv);
                getCode();
                break;
            case R.id.ceremail_right_away:
                if(phoneCerEdt.getText().toString().trim()==""||phoneCerEdt.getText().toString().trim().equals("")){
                         Toast.makeText(this,"请输入手机号",Toast.LENGTH_SHORT).show();
                        }
                else if(phoneCodeEt.getText().toString().trim()==""||phoneCodeEt.getText().toString().trim()==""){
                    Toast.makeText(this,"请输入验证码",Toast.LENGTH_SHORT).show();
                }
                else{
                    //绑定
                    getBind();
//                    Toast.makeText(this,"绑定成功",Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    public void getBind() {
        String code=phoneCodeEt.getText().toString().trim();
        String phone=phoneCerEdt.getText().toString().trim();
        String url=""+MainActivity.url+"act=goods&op=amobile&member_id="+ MainActivity.myLogin.getMemberid()+"&vcode="+code+"&mobile="+phone+"";
        Log.i("xiaogalade","bindurl=="+url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);

        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            String result = resultObject.optString("code");
                            String data=resultObject.optString("data");
                            if (result=="0"){
                                toast(data);
                                finish();
                            }else{
                                toast(data);
                            }

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();}
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }

    public  void getCode() {
        String phone=phoneCerEdt.getText().toString().trim();
//        toast(phone);
        String url=""+MainActivity.url+"act=goods&op=sendmsg_mobile&member_id="+MainActivity.myLogin.getMemberid()+"&mobile="+phone+"";
        Log.i("xiaogalade","url=="+url);
//        String url="http://www.xilingou.com/mobile/index.php?act=goods&op=send_bind_email&username=gagaha&email="+emailCerEdt.getText().toString().trim()+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);

        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            String result = resultObject.optString("state");
                            if (result=="true"){
                                toast("验证码发送成功");

                            }

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();}
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }
}
